package controller.log;

import DateTools.DateUtil;
import domain.entity.ExecutionLog;
import domain.entity.base.UserInfo;
import domain.service.base.ExecutionLogService;
import domain.service.base.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.WebContextUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bowen on 2016/7/28.
 */
@Controller
public class ExecutionLogController {

    private static final Logger logger = LoggerFactory.getLogger(ExecutionLogController.class);

    @Autowired
    private ExecutionLogService executionLogService;

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("getExecutionLogList")
    @ResponseBody
    public List<ExecutionLog> getExecutionLogList(){
        List<ExecutionLog> list = executionLogService.getExecutionLogList();
        for(ExecutionLog log : list){
            log.setCreate_time_txt(DateUtil.DateToString(log.getCreate_time(),"yyyy-MM-dd HH:mm:ss"));
        }
        return list;
    }

    @RequestMapping(value = "insertLog")
    public Map<String,Object> insertLog(String type){
        String userId = WebContextUtil.getCurrentUser().getUserid();
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        String log = "";
        if(userInfo!=null){
            if("facilities".equals(type)){

                ExecutionLog executionLog = new ExecutionLog();
                executionLog.setOpenid(userId);
                executionLog.setUsername(userInfo.getName());
                log = "用户"+userInfo.getName()+"于"+ DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss")+"查看基础设施信息";
                executionLog.setExe_log(log);
                executionLogService.insert(executionLog);
            }
            if("company".equals(type)){
                ExecutionLog executionLog = new ExecutionLog();
                executionLog.setOpenid(userId);
                executionLog.setUsername(userInfo.getName());
                log = "用户"+userInfo.getName()+"于"+ DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss")+"查看养护公司信息";
                executionLog.setExe_log(log);
                executionLogService.insert(executionLog);
            }
            if("base".equals(type)){
                ExecutionLog executionLog = new ExecutionLog();
                executionLog.setOpenid(userId);
                executionLog.setUsername(userInfo.getName());
                log = "用户"+userInfo.getName()+"于"+ DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss")+"查看养护基地信息";
                executionLog.setExe_log(log);
                executionLogService.insert(executionLog);
            }
            if("vehicle".equals(type)){
                ExecutionLog executionLog = new ExecutionLog();
                executionLog.setOpenid(userId);
                executionLog.setUsername(userInfo.getName());
                log = "用户"+userInfo.getName()+"于"+ DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss")+"查看车辆设施信息";
                executionLog.setExe_log(log);
                executionLogService.insert(executionLog);
            }
            if("plan".equals(type)){
                ExecutionLog executionLog = new ExecutionLog();
                executionLog.setOpenid(userId);
                executionLog.setUsername(userInfo.getName());
                log = "用户"+userInfo.getName()+"于"+ DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss")+"查看当日养护计划信息";
                executionLog.setExe_log(log);
                executionLogService.insert(executionLog);
            }
        }



        return new HashMap<String,Object>();
    }
}
